package com.zyy.smartcity1129.bus;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.Button;
import android.widget.ExpandableListView;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.core.content.ContextCompat;
import androidx.lifecycle.MutableLiveData;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.zyy.smartcity1129.BaseFragment;
import com.zyy.smartcity1129.R;
import com.zyy.smartcity1129.data.BusLineInfo;
import com.zyy.smartcity1129.data.BusStopInfo;
import com.zyy.smartcity1129.util.Http;

import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName: BusFragment$
 * @Description: 作用
 * @Author: letquit
 * @CreateDate: 4/12/2021$
 * @Version: 1.0
 */
public class BusFragment extends BaseFragment {
//    private RecyclerView rvOnly;
//    private Button btnNext;
    public static BusLineInfo.RowsBean line;
    public static List<BusStopInfo.RowsBean> stop;
    public static MutableLiveData<Boolean> liveData = new MutableLiveData<>();
    public static String path;
    public static double price;
    private ExpandableListView expandList;

    //    @Override
//    protected int layId() {
//        return R.layout.f_rv_only;
//    }
    @Override
    protected int layId() {
        return R.layout.f_expandlist;
    }

    @Override
    protected String title() {
        return "智慧巴士";
    }

    @Override
    protected boolean back() {
        return true;
    }

    @Override
    protected Boolean bottom() {
        return null;
    }

    @Override
    protected void init(View iv) {
        initView(iv);
        liveData.observe(getViewLifecycleOwner(), m -> {
            remove();
        });
//        rvOnly.setLayoutManager(new LinearLayoutManager(getContext()));
        th(() -> {
            List<BusLineInfo.RowsBean> list = Http.getInstance().get("/prod-api/api/bus/line/list", null, BusLineInfo.class, 1).getRows();
            List<List<BusStopInfo.RowsBean>> stopList = new ArrayList<>();
            for (BusLineInfo.RowsBean bean : list) {
                stopList.add(Http.getInstance().get("/prod-api/api/bus/stop/list?linesId=" + bean.getId(), null, BusStopInfo.class, 1).getRows());
            }
//            实现不出效果有bug就改用了折叠列表，但代码多了40行
//            RvAd<BusLineInfo.RowsBean> rvAd = new RvAd<BusLineInfo.RowsBean>(list, R.layout.rv_bus) {
//
//                private void initView(View iv) {
//                    tv1 = (TextView) iv.findViewById(R.id.tv1);
//                    tv2 = (TextView) iv.findViewById(R.id.tv2);
//                    tv3 = (TextView) iv.findViewById(R.id.tv3);
//                    tv4 = (TextView) iv.findViewById(R.id.tv4);
//                    tv5 = (TextView) iv.findViewById(R.id.tv5);
//                    ig = (ImageView) iv.findViewById(R.id.ig);
//                    rv = (RecyclerView) iv.findViewById(R.id.rv);
//                }
//
//                private RecyclerView rv;
//                private ImageView ig;
//                private TextView tv5;
//                private TextView tv4;
//                private TextView tv3;
//                private TextView tv2;
//                private TextView tv1;
//
//                @Override
//                protected void initR(View vi, BusLineInfo.RowsBean rowsBean, int p) {
//                    initView(vi);
//                    tv1.setText(rowsBean.getName());
//                    tv2.setText("起点：" + rowsBean.getFirst() + "  终点：" + rowsBean.getEnd());
//                    tv3.setText("运行时间：" + rowsBean.getStartTime() + "-" + rowsBean.getEndTime());
//                    tv4.setText("票价：" + rowsBean.getPrice() + "元");
//                    tv5.setText("里程：" + rowsBean.getMileage() + "km");
//                    ig.setOnClickListener(view1 -> {
//                        if (rv.getVisibility() == View.GONE) {
//                            rv.setVisibility(View.VISIBLE);
//                            ig.setBackgroundResource(R.drawable.right);
//                        } else {
//                            rv.setVisibility(View.GONE);
//                            ig.setBackgroundResource(R.drawable.down);
//                        }
//                    });
//                    int size = stopList.get(p).size();
//                    RvAd<BusStopInfo.RowsBean> rvAd1 = new RvAd<BusStopInfo.RowsBean>(stopList.get(p), R.layout.rv_text) {
//                        private void initView(View vi) {
//                            tv = (TextView) vi.findViewById(R.id.tv);
//                        }
//
//                        private TextView tv;
//
//                        @Override
//                        protected void initR(View vi, BusStopInfo.RowsBean rowsBean, int p1) {
//                            initView(vi);
//                            if (p1 == 0) {
//                                tv.setText("起点：" + rowsBean.getName());
//                            } else if (p1 == size) {
//                                tv.setText("终点：" + rowsBean.getName());
//                            } else {
//                                tv.setText("     " + rowsBean.getName());
//                            }
//                        }
//                    };
//                    rv.setLayoutManager(new LinearLayoutManager(getContext()));
//                    rv.setAdapter(rvAd1);
//                    vi.setOnClickListener(view1 -> {
//                        path = rowsBean.getName();
//                        price = rowsBean.getPrice();
//                        line = rowsBean;
//                        stop = stopList.get(p);
//                        add(new BusShowFragment());
//                    });
//                }
//            };
            ui(() -> {
                DiyExpandableListviewAdapter adapter = new DiyExpandableListviewAdapter(list, stopList, getContext());
//                rvOnly.setAdapter(rvAd);
                expandList.setAdapter(adapter);
//                默认展开第一个数组
//                expandList.expandGroup(0);
                expandList.setOnGroupClickListener(new ExpandableListView.OnGroupClickListener() {
                    @Override
                    public boolean onGroupClick(ExpandableListView expandableListView, View view, int i, long l) {
                        path = list.get(i).getName();
                        price = list.get(i).getPrice();
                        line = list.get(i);
                        stop = stopList.get((int) l);
                        add(new BusShowFragment());
                        return true;
                    }
                });
            });
        });
    }

    class DiyExpandableListviewAdapter extends BaseExpandableListAdapter {
        private List<BusLineInfo.RowsBean> groups;
        private List<List<BusStopInfo.RowsBean>> childs;
        private Context context;

        public DiyExpandableListviewAdapter(List<BusLineInfo.RowsBean> groups, List<List<BusStopInfo.RowsBean>> childs, Context context) {
            this.groups = groups;
            this.childs = childs;
            this.context = context;
        }

        @Override
        public int getGroupCount() {
            return groups.size();
        }

        @Override
        public int getChildrenCount(int i) {
            return childs.size();
        }

        @Override
        public Object getGroup(int i) {
            return groups.get(i);
        }

        @Override
        public Object getChild(int i, int i1) {
            return childs.get(i1);
        }

        @Override
        public long getGroupId(int i) {
            return i;
        }

        @Override
        public long getChildId(int i, int i1) {
            return i1;
        }

        @Override
        public boolean hasStableIds() {
            return true;
        }

        @Override
        public View getGroupView(int i, boolean b, View view, ViewGroup viewGroup) {
            GroupViewHolder groupViewHolder;
            if (view == null) {
                view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.rv_bus, viewGroup, false);
                groupViewHolder = new GroupViewHolder();
                groupViewHolder.tv1 = (TextView) view.findViewById(R.id.tv1);
                groupViewHolder.tv2 = (TextView) view.findViewById(R.id.tv2);
                groupViewHolder.tv3 = (TextView) view.findViewById(R.id.tv3);
                groupViewHolder.tv4 = (TextView) view.findViewById(R.id.tv4);
                groupViewHolder.tv5 = (TextView) view.findViewById(R.id.tv5);
                groupViewHolder.ig = (ImageView) view.findViewById(R.id.ig);
                groupViewHolder.rv = (RecyclerView) view.findViewById(R.id.rv);
                view.setTag(groupViewHolder);
            } else {
                groupViewHolder = (GroupViewHolder) view.getTag();
            }
            groupViewHolder.tv1.setText(groups.get(i).getName());
            groupViewHolder.tv2.setText("起点：" + groups.get(i).getFirst() + "  终点：" + groups.get(i).getEnd());
            groupViewHolder.tv3.setText("运行时间：" + groups.get(i).getStartTime() + "-" + groups.get(i).getEndTime());
            groupViewHolder.tv4.setText("票价：" + groups.get(i).getPrice() + "元");
            groupViewHolder.tv5.setText("里程：" + groups.get(i).getMileage() + "km");
            if (b) {
                groupViewHolder.ig.setImageDrawable(ContextCompat.getDrawable(context, R.drawable.right));
            } else {
                groupViewHolder.ig.setImageDrawable(ContextCompat.getDrawable(context, R.drawable.down));
            }
            return view;
        }

        @Override
        public View getChildView(int i, int i1, boolean b, View view, ViewGroup viewGroup) {
            ChildViewHolder childViewHolder;
            if (view == null) {
                view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.rv_text, viewGroup, false);
                childViewHolder = new ChildViewHolder();
                childViewHolder.tv = (TextView) view.findViewById(R.id.tv);
                view.setTag(childViewHolder);
            } else {
                childViewHolder = (ChildViewHolder) view.getTag();
            }
            if (i1 == childs.size()) {
                childViewHolder.tv.setText("终点：");
            } else if (i1 == 0) {
                childViewHolder.tv.setText("起点：");
            } else {
                childViewHolder.tv.setText("            ");
            }
            childViewHolder.tv.setText(childs.get(i).get(i1).getName());
            return view;
        }

        @Override
        public boolean isChildSelectable(int i, int i1) {
            return true;
        }

        class GroupViewHolder {
            RecyclerView rv;
            ImageView ig;
            TextView tv5;
            TextView tv4;
            TextView tv3;
            TextView tv2;
            TextView tv1;
        }

        class ChildViewHolder {
            TextView tv;
        }
    }

    private void initView(View iv) {
//        rvOnly = (RecyclerView) iv.findViewById(R.id.rvOnly);
//        btnNext = (Button) iv.findViewById(R.id.btnNext);
        expandList = (ExpandableListView) iv.findViewById(R.id.expand_list);
    }
}
